package com.guet.main.service.impl;

import com.guet.main.entity.Room;
import com.guet.main.entity.RoomReservation;
import com.guet.main.entity.RoomTimeSlice;
import com.guet.main.entity.User;
import com.guet.main.entity.vo.ReservationVo;
import com.guet.main.mapper.RoomReservationMapper;
import com.guet.main.service.RoomReservationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guet.main.service.RoomService;
import com.guet.main.service.RoomTimeSliceService;
import com.guet.main.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * room_reservation 服务实现类
 * </p>
 *
 * @author csp
 * @since 2022-07-29
 */
@Service
public class RoomReservationServiceImpl extends ServiceImpl<RoomReservationMapper, RoomReservation> implements RoomReservationService {
    @Autowired
    private  RoomReservationService roomReservationService;
    @Autowired
    private  RoomTimeSliceService roomTimeSliceService;
    @Autowired
    private RoomService roomService;
    @Autowired
    private UserService userService;


    @Override
    @Transactional
    public boolean reserve(ReservationVo reservationVo) {
        //创建预约申请
        RoomReservation roomReservation = new RoomReservation();
        roomReservation.setUserId(reservationVo.getUserId());
        roomReservation.setSliceId(reservationVo.getSliceId());
        roomReservation.setState(true);
        roomReservation.setCommit(reservationVo.getCommit());

        //修改时间片，预约人和状态
        RoomTimeSlice roomTimeSlice = roomTimeSliceService.getById(reservationVo.getSliceId());
        User user = userService.getById(reservationVo.getUserId());
        roomTimeSlice.setUserId(reservationVo.getUserId());
        roomTimeSlice.setUserName(user.getName());
        if (!roomTimeSlice.getState()){
            return false;
        }
        roomTimeSlice.setState(false);

        //修改会议室的预约次数
        Room room = roomService.getById(roomTimeSlice.getRoomId());
        if((room.getReserveNumber() - 1)<0){
            room.setReserveNumber(0);
            room.setState(false);
        }else {
            room.setReserveNumber(room.getReserveNumber()-1);
        }
        if(!room.getState()){
            return false;
        }

        //修改用户的预约次数
        if(user.getReserveNumber()-1<0){
            user.setReserveNumber(0);
            user.setState(false);
        }else{
            user.setReserveNumber(user.getReserveNumber()-1);
        }
        if(!user.getState()){
            return false;
        }
        roomReservationService.save(roomReservation);
        roomTimeSliceService.updateById(roomTimeSlice);
        userService.updateById(user);
        roomService.updateById(room);
        return true;
    }

    @Override
    @Transactional
    public boolean cancel(String id) {
        //修改预约申请状态
        RoomReservation roomReservation = roomReservationService.getById(id);
        roomReservation.setState(false);

        //修改时间片
        RoomTimeSlice roomTimeSlice = roomTimeSliceService.getById(roomReservation.getSliceId());
        roomTimeSlice.setUserId("");
        roomTimeSlice.setUserName("");
        roomTimeSlice.setState(true);

        //修改用户取消预约次数
        User user = userService.getById(roomReservation.getUserId());
        if(user.getReserveNumberCancel()-1<0){
            user.setReserveNumberCancel(0);
            user.setState(false);
        }else{
            user.setReserveNumberCancel(user.getReserveNumberCancel()-1);
        }
        roomReservationService.updateById(roomReservation);
        userService.updateById(user);
        roomTimeSliceService.updateById(roomTimeSlice);
        return user.getState();
    }
}
